﻿using Dapper;
using Management.Systeam.Domaina;
using Management.Systeam.ErrorCode;
using Management.Systeam.Infrastructure.Implement;
using Management.Systeam.Infrastructure.Interface;
using Management.Systeam.Read.Apis.Application.Command.Useres;
using Management.Systeam.Read.Apis.Auto.UserDto;
using MediatR;
using MySqlX.XDevAPI.Common;
using System.Text;

namespace Management.Systeam.Read.Apis.Application.CommandHandler.Useres
{
    public class ShowUsersCommandHandler : IRequestHandler<ShowUsersCommand, ApiPeaging<ApiResult<List<UserDtos>>>>
    {
        IBaseRepository<Users> Users;
        IBaseRepository<DepartMents> DepartMents;
        IBaseRepository<Role> Role;
      public   DatabaseContext db;

        public ShowUsersCommandHandler(IBaseRepository<Users> Users, IBaseRepository<DepartMents> DepartMents, IBaseRepository<Role> Role, DatabaseContext db)
        {
            this.Users = Users;
            this.DepartMents = DepartMents;
            this.Role = Role;
            this.db = db;
        }


        //测试
        public async Task<ApiPeaging<ApiResult<List<UserDtos>>>> Handle(ShowUsersCommand request, CancellationToken cancellationToken)
        {
            StringBuilder countSql = new StringBuilder();
            StringBuilder sb = new StringBuilder();
            sb.Append(@"
                SELECT
           a.UId,
         a.User_WorkCode,
         a.User_Name,
         a.User_PassWord,
         a.User_Email,
         a.User_Phone,
         a.User_FixedPhone,
         a.CreateTime,
         a.Login_Number,
         a.User_IsDel,        
         b.Role_Id,
         b.Role_Name,
         b.Role_CreateTime,
         b.Role_Content,
         b.Role_IsDel,
         b.Rid,
         c.Department_Id,
         c.Department_Name,
         c.Parent_Department,
         c.Department_IsDel
                FROM
	                users a
	              JOIN role b ON a.Role_Id = b.Role_Id
JOIN departments c ON a.Department_Id = c.Department_Id
                WHERE
	                a.IsDel = 0 
	                AND b.IsDel = 0 
	                AND c.IsDel = 0
                ");
            countSql.Append(@"SELECT
                  COUNT(a.UId)
                       FROM
	                users a
	              JOIN role b ON a.Role_Id = b.Role_Id
JOIN departments c ON a.Department_Id = c.Department_Id
                WHERE
	                a.IsDel = 0 
	                AND b.IsDel = 0 
	                AND c.IsDel = 0");

            if (request.User_Name != null)
            {
                sb.Append(" AND a.User_Name LIKE '%" + request.User_Name + "%'");
                countSql.Append(" AND a.User_Name LIKE '%" + request.User_Name + "%'");
            }
            if (request.User_WorkCode != null)
            {
                sb.Append(" AND a.User_WorkCode LIKE '%" + request.User_WorkCode + "%'");
                countSql.Append(" AND a.User_WorkCode LIKE '%" + request.User_WorkCode + "%'");
            }
            if (request.Department_Id != null)
            {
                sb.Append(" AND a.Department_Id LIKE '%" + request.Department_Id + "%'");
                countSql.Append(" AND a.Department_Id LIKE '%" + request.Department_Id + "%'");
            }
            if (request.Role_Id != null)
            {
                sb.Append(" AND a.Role_Id LIKE '%" + request.Role_Id + "%'");
                countSql.Append(" AND a.Role_Id LIKE '%" + request.Role_Id + "%'");
            }
            // 添加排序和分页
            sb.Append(" ORDER BY a.CreateTime DESC LIMIT @PageSize OFFSET @offset");

            // 设置参数
            var parame= new
            {
                User_Name = request.User_Name,
                User_WorkCode = request.User_WorkCode,
                Department_Id = request.Department_Id,
                Role_Id = request.Role_Id,
                PageSize = request.PageSize,
                offset = (request.PageIndex - 1) * request.PageSize
            };

            // 执行查询
            var pageData = await db.DapperConnection.QueryAsync<UserDtos>(sb.ToString(), parame);
            int total = await db.DapperConnection.ExecuteScalarAsync<int>(countSql.ToString(), parame);
            int totalPage = (int)Math.Ceiling(total / (double)request.PageSize);
            return new ApiPeaging<ApiResult<List<UserDtos>>>
            {
                Total = total,
                TotalPage = totalPage,
                Data = new ApiResult<List<UserDtos>>
                {
                    Code = ApiEnum.success,
                    Message = "查询成功",
                    Data = pageData.ToList()
                }
            };
        }
    }
}
